package com.hjm.project.walletDetails.walletDetails.utils;

import com.alibaba.fastjson.JSON;
import com.hjm.common.utils.LogUtils;
import com.hjm.common.utils.request.AutoLoadData;
import com.hjm.common.utils.request.DateUtils;
import com.hjm.common.utils.request.HttpClientUtils;
import com.hjm.framework.web.domain.AjaxResult;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.hjm.project.walletDetails.walletDetails.utils.DigestUtils.encodeShaHexStr;


public class SendUtils {
    public static final String zz_url="http://localhost:8082/abc-pro/agent/addAgentMoney.do";
    public static final String adjust_url="http://localhost:8082/abc-pro/agent/adjustAgentMoney.do";
    public static final String tx_url="http://localhost:8082/abc-pro/agent/agentReflect.do";
    public static final String remit_url="http://localhost:8082/abc-pro/agent/agenRemit.do";
    public static final String balance_url="http://localhost:8082/abc-pro/agent/getAgentMoney.do";
    public static final String qry_url="http://localhost:8082/abc-pro/agent/getOrderInfoBySerial.do";
    public static final String new_out_url="http://localhost:8082/abc-pro/agent/orderByNewOut.do";
    public static final String ybTest_url="http://localhost:8082/abc-pro/agent/ybTest.do";

    public static final String aesKey = "d04bd2837681c6d308a4990c60b3a6c2";

    static ExecutorService es = Executors.newFixedThreadPool(20);
    public static void executeTask() {
        try {
            for (int i = 1; i <= 20; i++) {
                es.execute(createTask(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static Runnable createTask(int i) {
        return new Runnable() {
            @Override
            public void run() {
                try {
                    tiXian(i);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
    }


    public static void main(String[] args) {
//        System.out.println(RandomStringUtils.random(32, new char[]{'a','b','c','d','e','f', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}));
//        balance();      //代理虚拟余额查询
        tiXian(1);     //提现
        zhuanZhang();  //分润累加
        //adjustAgentMoney();  //账户余额调整
//          remit();      //冻结余额调减
//        qry();        //代付查询
//        newOut();     //失败补出
//        wage();
//        ybTest();

//            for(int i = 1; i <= 1; i ++){
//                int finalI = i;
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        tiXian(finalI);
//                    }
//                }).start();
//            }


//        executeTask();
    }



    public static void zhuanZhang() {
        try {
            Map map = new HashMap();
            map.put("agentNum", "100015744");   //100032739
            map.put("amt", "400000");
            map.put("orderNum", "P" + DateUtils.getNumberDateTime());
            map.put("userType", "3");
            map.put("merType", "4");
            Map map1 = new HashMap();
            map1.put("actype", "3");
            map1.put("type", "0");
            map1.put("amount", "100000");
            map1.put("remarks", "通讯费");
            map1.put("orderDate", "2024-01-19");
            Map map2 = new HashMap();
            map2.put("actype", "4");
            map2.put("type", "0");
            map2.put("amount", "100000");
            map2.put("remarks", "服务费");
            map2.put("orderDate", "2024-01-19");
            Map map3 = new HashMap();
            map3.put("actype", "2");
            map3.put("type", "0");
            map3.put("amount", "100000");
            map3.put("remarks", "达标");
            map3.put("orderDate", "2024-01-19");
            Map map4 = new HashMap();
            map4.put("actype", "1");
            map4.put("type", "0");
            map4.put("amount", "100000");
            map4.put("remarks", "分润");
            map4.put("orderDate", "2024-01-19");

            JSONArray jsonArray = new JSONArray();
            jsonArray.add(JSONObject.fromObject(map1));
            jsonArray.add(JSONObject.fromObject(map2));
            jsonArray.add(JSONObject.fromObject(map3));
            jsonArray.add(JSONObject.fromObject(map4));

            map.put("list", jsonArray.toString());
            map.put("sign", encodeShaHexStr(map));
            Map hasMap = new HashMap();
            //加密值
            JSONObject jsonObject = JSONObject.fromObject(map);
            System.out.println(jsonObject);
            hasMap.put("data", AESUtil.encrypt(jsonObject.toString(), aesKey));
            com.alibaba.fastjson.JSONObject json = HttpClientUtils.sendHttpPostForm(zz_url, hasMap);
            String outerLayer = json.getString("responseVal");
            String code = json.getString("responseCode");
            if ("200".equals(code)) {
                Map maps = (Map) JSON.parse(outerLayer);
                String innerLayer = AESUtil.decrypt(maps.get("data").toString(), aesKey);
                Map tomap = (Map) JSON.parse(innerLayer);
                if ("200".equals(tomap.get("code") + "")) {
                    String sign = tomap.get("sign").toString();
                    tomap.remove("sign");
                    tomap.remove("msg");
                    tomap.remove("code");
                    if (StringUtils.isEmpty(sign) || !encodeShaHexStr(tomap).equals(sign)) {
                        System.out.println("签名验证失败");
                    } else {
                        //签名成功 -- 业务逻辑处理
                        //                        String var = AESUtil.decrypt(tomap.get("resultData").toString(),"98219030384df694a1db622d0541047c");
                        String var = tomap.get("resultData").toString();
                        Map hasMaps = (Map) JSON.parse(var);
                        System.out.println(hasMaps);
                        System.out.println(hasMaps.get("merId"));
                    }
                } else {
                    System.out.println("操作失败！");
                    System.out.println(tomap.get("msg"));
                    //
                }
            } else {
                System.out.println("请求失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void tiXian(int i) {
        try {
            Map map = new HashMap();
            map.put("agentNum", "100015740"); //代理号
            map.put("amt", "2000");  //单位分
            map.put("type", "2");     //账户类型 0 分润提现  1返现提现 2通讯费提现 3服务费提现
            map.put("orderNum", "S" + DateUtils.getNumberDateTime()+i); //流水
            map.put("sign", encodeShaHexStr(map));
            Map hasMap = new HashMap();
            //加密值
            JSONObject jsonObject = JSONObject.fromObject(map);
            System.out.println(jsonObject);
            hasMap.put("data", AESUtil.encrypt(jsonObject.toString(), aesKey));
            com.alibaba.fastjson.JSONObject json = HttpClientUtils.sendHttpPostForm(tx_url, hasMap);
            System.out.println("响应报文:"+json);
            String outerLayer = json.getString("responseVal");
            String code = json.getString("responseCode");
            if ("200".equals(code)) {
                Map maps = (Map) JSON.parse(outerLayer);
                String innerLayer = AESUtil.decrypt(maps.get("data").toString(), aesKey);
                Map tomap = (Map) JSON.parse(innerLayer);
                System.out.println("解密响应报文:"+tomap);
                if ("200".equals(tomap.get("code") + "")) {
                    String sign = tomap.get("sign").toString();
                    tomap.remove("sign");
                    tomap.remove("msg");
                    tomap.remove("code");
                    if (StringUtils.isEmpty(sign) || !encodeShaHexStr(tomap).equals(sign)) {
                        System.out.println("签名验证失败");
                    } else {
                        //签名成功 -- 业务逻辑处理
                        //                        String var = AESUtil.decrypt(tomap.get("resultData").toString(),"98219030384df694a1db622d0541047c");
                        String var = tomap.get("resultData").toString();
                        Map hasMaps = (Map) JSON.parse(var);
                        System.out.println(hasMaps);
                        System.out.println(hasMaps.get("merId"));
                    }
                } else {
                    System.out.println("操作失败！");
                    System.out.println(tomap.get("msg"));
                    //
                }
            } else {
                System.out.println("请求失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 调整账户
     * @param agentNum 代理号
     * @param money 金额分
     * @param adjustType 0 调减 1 调增
     * @param accType 账户类型 0 分润  1返现 2通讯费 3服务费
     *
     */
    public static AjaxResult adjustAgentMoney(String agentNum, String money, String adjustType, String accType) {
        try {
            Map map = new HashMap();
            map.put("agentNum", agentNum); //代理号
            map.put("amt", new BigDecimal(money).multiply(new BigDecimal("100")).toString()); //金额分
            map.put("adjustType",adjustType); //0 调减 1 调增
            map.put("actype", accType);     //账户类型 1 分润  2返现 3通讯费 4服务费
            map.put("orderNum", "S" + DateUtils.getNumberDateTime());
            map.put("sign", encodeShaHexStr(map));
            Map hasMap = new HashMap();
            //加密值
            JSONObject jsonObject = JSONObject.fromObject(map);
            LogUtils.logInfo("请求参数:" + jsonObject.toString());
            System.out.println(jsonObject);
            hasMap.put("data", AESUtil.encrypt(jsonObject.toString(), aesKey));
            com.alibaba.fastjson.JSONObject json = HttpClientUtils.sendHttpPostForm(AutoLoadData.adjust_url, hasMap);
            LogUtils.logInfo("响应报文:" + json);
            String outerLayer = json.getString("responseVal");
            String code = json.getString("responseCode");
            if ("200".equals(code)) {
                Map maps = (Map) JSON.parse(outerLayer);
                String innerLayer = AESUtil.decrypt(maps.get("data").toString(), aesKey);
                Map tomap = (Map) JSON.parse(innerLayer);
                System.out.println("解密响应报文:"+tomap);
                if ("200".equals(tomap.get("code") + "")) {
                    String sign = tomap.get("sign").toString();
                    tomap.remove("sign");
                    tomap.remove("msg");
                    tomap.remove("code");
                    if (StringUtils.isEmpty(sign) || !encodeShaHexStr(tomap).equals(sign)) {
                        return AjaxResult.error("签名验证失败");
                    } else {
                        //签名成功 -- 业务逻辑处理
                        //String var = AESUtil.decrypt(tomap.get("resultData").toString(),"98219030384df694a1db622d0541047c");
                        String var = tomap.get("resultData").toString();
                        Map hasMaps = (Map) JSON.parse(var);
                        if (Integer.parseInt(hasMaps.get("status").toString()) == 1) {
                            return AjaxResult.error(hasMaps.get("msg").toString());
                        }
                        System.out.println(hasMaps);
                        System.out.println(hasMaps.get("merId"));
                    }
                } else {
                    return AjaxResult.error(tomap.get("msg").toString());
                }
            } else {
                return AjaxResult.error("请求失败！");

            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("调整异常！");

        }
        return AjaxResult.success();
    }

    public static void remit() {
        try {
            Map map = new HashMap();
            map.put("agentNum", "100032725");
            map.put("amt", "300");
            map.put("orderNum", "S" + DateUtils.getNumberDateTime());
            map.put("sign", encodeShaHexStr(map));
            Map hasMap = new HashMap();
            //加密值
            JSONObject jsonObject = JSONObject.fromObject(map);
            System.out.println(jsonObject);
            hasMap.put("data", AESUtil.encrypt(jsonObject.toString(), aesKey));
            com.alibaba.fastjson.JSONObject json = HttpClientUtils.sendHttpPostForm(remit_url, hasMap);
            System.out.println("响应报文:"+json);
            String outerLayer = json.getString("responseVal");
            String code = json.getString("responseCode");
            if ("200".equals(code)) {
                Map maps = (Map) JSON.parse(outerLayer);
                String innerLayer = AESUtil.decrypt(maps.get("data").toString(), aesKey);
                Map tomap = (Map) JSON.parse(innerLayer);
                System.out.println("解密响应报文:"+tomap);
                if ("200".equals(tomap.get("code") + "")) {
                    String sign = tomap.get("sign").toString();
                    tomap.remove("sign");
                    tomap.remove("msg");
                    tomap.remove("code");
                    if (StringUtils.isEmpty(sign) || !encodeShaHexStr(tomap).equals(sign)) {
                        System.out.println("签名验证失败");
                    } else {
                        //签名成功 -- 业务逻辑处理
                        //                        String var = AESUtil.decrypt(tomap.get("resultData").toString(),"98219030384df694a1db622d0541047c");
                        String var = tomap.get("resultData").toString();
                        Map hasMaps = (Map) JSON.parse(var);
                        System.out.println(hasMaps);
                        System.out.println(hasMaps.get("merId"));
                    }
                } else {
                    System.out.println("操作失败！");
                    System.out.println(tomap.get("msg"));
                    //
                }
            } else {
                System.out.println("请求失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void qry() {
        try {
            Map map = new HashMap();
            map.put("settleSerial", "QCK20210224151710000000000004306");
            map.put("sign", encodeShaHexStr(map));
            Map hasMap = new HashMap();
            //加密值
            JSONObject jsonObject = JSONObject.fromObject(map);
            System.out.println(jsonObject);
            hasMap.put("data", AESUtil.encrypt(jsonObject.toString(), aesKey));
            com.alibaba.fastjson.JSONObject json = HttpClientUtils.sendHttpPostForm(qry_url, hasMap);
            System.out.println("响应报文:"+json);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void newOut() {
        try {
            Map map = new HashMap();
            map.put("settleSerial", "DL920220616160610000000000004684");
            map.put("sign", encodeShaHexStr(map));
            Map hasMap = new HashMap();
            //加密值
            JSONObject jsonObject = JSONObject.fromObject(map);
            System.out.println(jsonObject);
            hasMap.put("data", AESUtil.encrypt(jsonObject.toString(), aesKey));
            com.alibaba.fastjson.JSONObject json = HttpClientUtils.sendHttpPostForm(new_out_url, hasMap);
            System.out.println("响应报文:"+json);
            String outerLayer = json.getString("responseVal");
            String code = json.getString("responseCode");
            if ("200".equals(code)) {
                Map maps = (Map) JSON.parse(outerLayer);
                String innerLayer = AESUtil.decrypt(maps.get("data").toString(), aesKey);
                Map tomap = (Map) JSON.parse(innerLayer);
                System.out.println("解密响应报文:"+tomap);
                if ("200".equals(tomap.get("code") + "")) {
                    String sign = tomap.get("sign").toString();
                    tomap.remove("sign");
                    tomap.remove("msg");
                    tomap.remove("code");
                    if (StringUtils.isEmpty(sign) || !encodeShaHexStr(tomap).equals(sign)) {
                        System.out.println("签名验证失败");
                    } else {
                        //签名成功 -- 业务逻辑处理
                        //                        String var = AESUtil.decrypt(tomap.get("resultData").toString(),"98219030384df694a1db622d0541047c");
                        String var = tomap.get("resultData").toString();
                        Map hasMaps = (Map) JSON.parse(var);
                        System.out.println(hasMaps);
                        System.out.println(hasMaps.get("merId"));
                    }
                } else {
                    System.out.println("操作失败！");
                    System.out.println(tomap.get("msg"));
                    //
                }
            } else {
                System.out.println("请求失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void balance() {
        try {
            /**
             avlBal 分润
             balance 达标返现
             simBal 通讯费返现
             fwBal 服务费返现 */
            Map map = new HashMap();
            map.put("agentNum", "100015740");
            map.put("userType", "1"); //用户来源：1_前置2_平台3_分润
            map.put("accountType", "0"); // 0 代理商
            map.put("sign", encodeShaHexStr(map));
            Map hasMap = new HashMap();
            //加密值
            JSONObject jsonObject = JSONObject.fromObject(map);
            System.out.println(jsonObject);
            hasMap.put("data", AESUtil.encrypt(jsonObject.toString(), aesKey));
            com.alibaba.fastjson.JSONObject json = HttpClientUtils.sendHttpPostForm(balance_url, hasMap);
            System.out.println("响应报文:"+json);
            String outerLayer = json.getString("responseVal");
            String code = json.getString("responseCode");
            if ("200".equals(code)) {
                Map maps = (Map) JSON.parse(outerLayer);
                String innerLayer = AESUtil.decrypt(maps.get("data").toString(), aesKey);
                Map tomap = (Map) JSON.parse(innerLayer);
                System.out.println("解密响应报文:"+tomap);
                if ("200".equals(tomap.get("code") + "")) {
                    String sign = tomap.get("sign").toString();
                    tomap.remove("sign");
                    tomap.remove("msg");
                    tomap.remove("code");
                    if (StringUtils.isEmpty(sign) || !encodeShaHexStr(tomap).equals(sign)) {
                        System.out.println("签名验证失败");
                    } else {
                        //签名成功 -- 业务逻辑处理
                        //                        String var = AESUtil.decrypt(tomap.get("resultData").toString(),"98219030384df694a1db622d0541047c");
                        String var = tomap.get("resultData").toString();
                        Map hasMaps = (Map) JSON.parse(var);
                        System.out.println(hasMaps);
                        System.out.println(hasMaps.get("merId"));
                    }
                } else {
                    System.out.println("操作失败！");
                    System.out.println(tomap.get("msg"));
                    //
                }
            } else {
                System.out.println("请求失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void ybTest() {
        try {
            Map map = new HashMap();
            map.put("id", "1");
            map.put("sign", encodeShaHexStr(map));
            Map hasMap = new HashMap();
            //加密值
            JSONObject jsonObject = JSONObject.fromObject(map);
            System.out.println(jsonObject);
            hasMap.put("data", AESUtil.encrypt(jsonObject.toString(), aesKey));
            com.alibaba.fastjson.JSONObject json = HttpClientUtils.sendHttpPostForm(ybTest_url, hasMap);
            System.out.println("响应报文:"+json);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


//    public static void main(String[] args) {
//        String key = "723ec0a9c317d85324f6be2e8ec61b7f";
//        String decryptAccount="10000";
//        String encryptAccount = AESUtil.encrypt(decryptAccount,  AESUtil.encrypt("100000001", key));
//        System.out.println(encryptAccount);
//
//        String decryptData = AESUtil.decrypt("AA94DA2E3B1A7B91573556981FE70502", AESUtil.encrypt("100000001", key));
//        System.out.println(decryptData);
//    }
}
